/* Include */
#include "../APP.h"
#include "ui/ui.h"

/* String buffer */
String UI_JSONcode = "";

/* --------------------------------- Between tasks --------------------------------- */

// JSON_Message JSON_MSG_RX;
struct JSON_Message* p_JSON_MSG_RX;

/* --------------------------------- Between tasks --------------------------------- */


/* Function header */
void Task_App_GUI(void* pvParameters);
void Receive_MSG_from_Queue();
void update_GUI();
void update_gui_JSON_code();
void update_gui_Servo_value();


/* App entrance */
void App_GUI()
{
    /* App init */
    HAL_LOG.println("[App_GUI] Initializing");

    /* Create startup thread */
    xTaskCreate(
        Task_App_GUI,
        "App_GUI",
        2048,
        NULL,
        0,
        NULL
    );
}


/* App task */
void Task_App_GUI(void* pvParameters)
{
    (void) pvParameters;

    /* UI init */
    ui_init();

    /* Wait a while */
    delay(1000);

    /* Loop */
    while (1)
    {
        Receive_MSG_from_Queue();
        // delay(50);
    }
    
    /* End of App_GUI */
    HAL_LOG.println("[App_GUI] Exit");
    vTaskDelete(NULL);
}


/* Recive message from App_JSON */
void Receive_MSG_from_Queue()
{
    if (Queue_2_App_GUI != 0) {
        if ( xQueueReceive(Queue_2_App_GUI, &( p_JSON_MSG_RX ), portMAX_DELAY) ) {
            /* Update GUI */
            update_GUI();
        }
    }
}


/* Update GUI information */
void update_GUI()
{
    update_gui_JSON_code();
    update_gui_Servo_value();
}


/* Update GUI JSON code */
void update_gui_JSON_code()
{
    int i = 0;
    UI_JSONcode = p_JSON_MSG_RX->Codehead;
    UI_JSONcode += '\n';
    for ( i = 0; i < 8; i++) {
        UI_JSONcode += p_JSON_MSG_RX->Servo_L[i];
        UI_JSONcode += ' ';
    }
    UI_JSONcode += '\n';
    for ( i = 0; i < 8; i++)
    {
        UI_JSONcode += p_JSON_MSG_RX->Servo_R[i];
        UI_JSONcode += ' ';
    }

    /* JSON code label */
    lv_label_set_text(ui_LabelRS485CODE, UI_JSONcode.c_str());
}


/* Update GUI Servo values */
void update_gui_Servo_value()
{
    /* Servo lrft */
    // lv_label_set_text(ui_LabelL1, String( "L-1:" + (p_JSON_MSG_RX->Servo_L[0])).c_str());
    lv_label_set_text(ui_LabelL1, (String("#0000ff L-1 :# ") + p_JSON_MSG_RX->Servo_L[0]).c_str());
    lv_label_set_text(ui_LabelL2, (String("#0000ff L-2 :# ") + p_JSON_MSG_RX->Servo_L[1]).c_str());
    lv_label_set_text(ui_LabelL3, (String("#0000ff L-3 :# ") + p_JSON_MSG_RX->Servo_L[2]).c_str());
    lv_label_set_text(ui_LabelL4, (String("#0000ff L-4 :# ") + p_JSON_MSG_RX->Servo_L[3]).c_str());
    lv_label_set_text(ui_LabelL5, (String("#0000ff L-5 :# ") + p_JSON_MSG_RX->Servo_L[4]).c_str());
    lv_label_set_text(ui_LabelL6, (String("#0000ff L-6 :# ") + p_JSON_MSG_RX->Servo_L[5]).c_str());
    lv_label_set_text(ui_LabelL7, (String("#0000ff L-7 :# ") + p_JSON_MSG_RX->Servo_L[6]).c_str());
    lv_label_set_text(ui_LabelL8, (String("#0000ff L-8 :# ") + p_JSON_MSG_RX->Servo_L[7]).c_str());

    /* Servo right */
    lv_label_set_text(ui_LabelR1, (String("#0000ff R-1 :# ") + p_JSON_MSG_RX->Servo_R[0]).c_str());
    lv_label_set_text(ui_LabelR2, (String("#0000ff R-2 :# ") + p_JSON_MSG_RX->Servo_R[1]).c_str());
    lv_label_set_text(ui_LabelR3, (String("#0000ff R-3 :# ") + p_JSON_MSG_RX->Servo_R[2]).c_str());
    lv_label_set_text(ui_LabelR4, (String("#0000ff R-4 :# ") + p_JSON_MSG_RX->Servo_R[3]).c_str());
    lv_label_set_text(ui_LabelR5, (String("#0000ff R-5 :# ") + p_JSON_MSG_RX->Servo_R[4]).c_str());
    lv_label_set_text(ui_LabelR6, (String("#0000ff R-6 :# ") + p_JSON_MSG_RX->Servo_R[5]).c_str());
    lv_label_set_text(ui_LabelR7, (String("#0000ff R-7 :# ") + p_JSON_MSG_RX->Servo_R[6]).c_str());
    lv_label_set_text(ui_LabelR8, (String("#0000ff R-8 :# ") + p_JSON_MSG_RX->Servo_R[7]).c_str());
}
